﻿CREATE TRIGGER trs_StateFlow
ON [dbo].[t_StateFlow]
FOR INSERT,UPDATE,DELETE
AS

EXEC admin.sec_CheckStateEditAccess

IF EXISTS(SELECT * FROM inserted) AND
	 NOT EXISTS(SELECT * FROM t_State AS STATE_FROM
							 INNER JOIN t_State AS STATE_TO ON STATE_FROM.TableName = STATE_TO.TableName
							 INNER JOIN inserted AS STATE_FLOW ON STATE_TO.ID = STATE_FLOW.StateToID AND
																											 STATE_FROM.ID = STATE_FLOW.StateFromID
							 )
BEGIN
	ROLLBACK TRAN
	RAISERROR('States must belong to same table.',16,1)
	RETURN
END

--INSERT INTO t_StateFlow_Groups(StateFromID,StateToID,GroupName) SELECT StateFromID,StateToID,'Admin' FROM inserted




